package common.tool.box.web.controller.utils.file;

import common.tool.box.common.core.domain.AjaxResult;
import common.tool.box.framework.web.service.SysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/utils/file")
@Api(tags = "file")
public class FileController {
    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private SysFileService sysFileService;

    /**
     * 获取登陆用户文件存储信息
     */
    @ApiOperation("获取登陆用户文件存储信息")
    @GetMapping("/getInfo")
    public AjaxResult getLoginUserFileInfo() {
        try {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("result", sysFileService.getUserFilesInfo());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据文件路径修改文件名
     */
    @ApiOperation("根据文件路径修改文件名")
    @GetMapping("/renameByFilePath")
    public AjaxResult renameByFilePath(String filePath, String newFileName) {
        try {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("result", sysFileService.renameByFilePath(filePath, newFileName));
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据文件路径批量删除文件
     */
    @ApiOperation("根据文件路径批量删除文件")
    @DeleteMapping("/deleteByFilePaths")
    public AjaxResult deleteByFilePaths(String[] filePaths) {
        try {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("result", sysFileService.deleteByFilePaths(filePaths));
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
